<?php

namespace App\Kuafu\Customer\Model;

use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use Illuminate\Database\Eloquent\Builder;
use App\Kuafu\Base\Model\BaseModel;

class CustomerModel extends BaseModel
{
    use KfSoftDeletes;

    protected $connection = 'mysql';
    protected $table      = 'customers'; //表名
    protected $primaryKey = 'id';
    protected $fillable   = [
        'name',
        'mobile',
        'salt',
        'password',
        'headImage',
        'lastLoginAt',
        'lastLoginIpAt',
        'createdAt',
        'updatedAt',
        'loginTimes'
    ];

    protected $hidden = [
        'updatedAt',
        'isDeleted'
    ];

    /**
     * 添加用户
     * @param array $data
     * @return Builder|\Illuminate\Database\Eloquent\Model
     */
    public static function addData(array $data)
    {
        return self::query()->create($data);
    }

    /**
     * 通过指定列值查询指定的数据
     * @param $columnValue
     * @param $field
     * @return Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public static function getUserInfoByColumn($columnValue, $field)
    {
        return self::query()
                   ->where($columnValue)
                   ->select($field)
                   ->first();
    }

    /**
     * 根据用户id修改数据
     * @param $userId
     * @param $data
     * @return int
     */
    public static function setCustomer($userId, $data)
    {
        return self::query()
                   ->where('id', '=', $userId)
                   ->update($data);
    }

    public function getHiddenMobileAttribute()
    {
        return substr_replace($this->mobile, '****', 3, 4);
    }
}
